package org.poptest.model;

import org.poptest.processor.Phase;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @author Alan Roche
 *         Date: 26/01/11
 *         Time: 21:33
 */
public class Context {
    private Phase phase;
    private final TestInstance testInstance;
    private Method method;
    private FieldNode fieldNode;

    public Context(final Object instance, final Method method) {
        this(instance);
        this.method = method;
    }

    public Context(final Class<?> testClass) {
        this.testInstance = new TestInstance(testClass);
    }

    public Context(final Object target) {
        this.testInstance = new TestInstance(target);
    }

    public TestInstance getTestInstance() {
        return testInstance;
    }

    public Class<?> getTestClass() {
        return testInstance.getTestClass();
    }

    public Method getMethod() {
        return method;
    }

    public FieldNode getFieldNode() {
        return fieldNode;
    }

    public void setFieldNode(FieldNode fieldNode) {
        this.fieldNode = fieldNode;
    }

    public Phase getPhase() {
        return phase;
    }

    public void setPhase(Phase phase) {
        this.phase = phase;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Context context = (Context) o;

        if (fieldNode != null ? !fieldNode.equals(context.fieldNode) : context.fieldNode != null) return false;
        if (method != null ? !method.equals(context.method) : context.method != null) return false;
        if (phase != context.phase) return false;
        if (testInstance != null ? !testInstance.equals(context.testInstance) : context.testInstance != null)
            return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = phase != null ? phase.hashCode() : 0;
        result = 31 * result + (testInstance != null ? testInstance.hashCode() : 0);
        result = 31 * result + (method != null ? method.hashCode() : 0);
        result = 31 * result + (fieldNode != null ? fieldNode.hashCode() : 0);
        return result;
    }
}
